# 生成1-100的随机数字
$targetNumber = Get-Random -Minimum 1 -Maximum 101
$minRange = 1
$maxRange = 100
$attempts = 0

Write-Host "我已经想好了一个1-100之间的数字，开始猜吧！" -ForegroundColor Green

do {
    # 显示当前猜测范围
    Write-Host "当前范围: [$minRange - $maxRange]" -ForegroundColor Yellow
    
    # 获取用户输入
    $guess = Read-Host "请输入你的猜测"
    $attempts++
    
    # 验证输入是否为数字
    if (-not ($guess -match '^\d+$')) {
        Write-Host "请输入有效的数字！" -ForegroundColor Red
        continue
    }
    
    $guess = [int]$guess
    
    # 检查猜测是否在有效范围内
    if ($guess -lt $minRange -or $guess -gt $maxRange) {
        Write-Host "请输入当前范围内的数字 [$minRange - $maxRange]！" -ForegroundColor Red
        continue
    }
    
    # 判断猜测结果
    if ($guess -eq $targetNumber) {
        Write-Host "恭喜！你猜对了！数字是 $targetNumber" -ForegroundColor Green
        Write-Host "总共尝试了 $attempts 次" -ForegroundColor Green
    }
    elseif ($guess -lt $targetNumber) {
        Write-Host "猜小了！" -ForegroundColor Blue
        $minRange = [Math]::Max($minRange, $guess + 1)
    }
    else {
        Write-Host "猜大了！" -ForegroundColor Magenta
        $maxRange = [Math]::Min($maxRange, $guess - 1)
    }
    
} while ($guess -ne $targetNumber)